package com.hanthink.common.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 * 
 * @author hanthink
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    
    private static final String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }
    
    /**
     * 获取当前班次开始结束时间
     * 
     * @return Date[]
     */
    public static Date[] getShiftDate()
    {
    	Date[] shiftDate = new Date[2];
    	Calendar curretTime = Calendar.getInstance();
    	
    	Calendar compareTime = Calendar.getInstance();
    	compareTime.set(Calendar.HOUR_OF_DAY, 8);
    	compareTime.set(Calendar.MINUTE, 30);
    	compareTime.set(Calendar.SECOND, 0);
    	compareTime.set(Calendar.MILLISECOND,0);
    	
     	Calendar compareTime2 = Calendar.getInstance();
    	compareTime2.set(Calendar.HOUR_OF_DAY, 20);
    	compareTime2.set(Calendar.MINUTE, 30);
    	compareTime2.set(Calendar.SECOND, 0);
    	compareTime2.set(Calendar.MILLISECOND,0);
    	
    	if(curretTime.compareTo(compareTime) < 0){
    		Calendar startTime = Calendar.getInstance();
    		startTime.add(Calendar.DAY_OF_MONTH, -1);
    		startTime.set(Calendar.HOUR_OF_DAY, 20);
    		startTime.set(Calendar.MINUTE, 30);
    		startTime.set(Calendar.SECOND, 0);
    		startTime.set(Calendar.MILLISECOND,0);
    		shiftDate[0] = startTime.getTime();
    		shiftDate[1] = compareTime.getTime();
        	
    	}else if(curretTime.compareTo(compareTime) >= 0 && curretTime.compareTo(compareTime2) < 0) {
    		shiftDate[0] = compareTime.getTime();
        	Calendar endTime = Calendar.getInstance();
        	endTime.set(Calendar.HOUR_OF_DAY, 20);
        	endTime.set(Calendar.MINUTE, 30);
        	endTime.set(Calendar.SECOND, 0);
        	endTime.set(Calendar.MILLISECOND,0);
    		shiftDate[1] = endTime.getTime();
    	}else {
    		shiftDate[0] = compareTime2.getTime();
    		Calendar endTime = Calendar.getInstance();
    		endTime.add(Calendar.DAY_OF_MONTH, 1);
        	endTime.set(Calendar.HOUR_OF_DAY, 8);
        	endTime.set(Calendar.MINUTE, 30);
        	endTime.set(Calendar.SECOND, 0);
        	endTime.set(Calendar.MILLISECOND,0);
    		shiftDate[1] = endTime.getTime();
    	}
        return shiftDate;
    }

    public static  Date[] getShiftDateQC(){
        Date[] shiftDate = new Date[2];
        Calendar curretTime = Calendar.getInstance();

        Calendar compareTime = Calendar.getInstance();
        compareTime.set(Calendar.MONTH,curretTime.get(Calendar.MONTH));
        compareTime.set(Calendar.DAY_OF_MONTH, 1);
        compareTime.set(Calendar.HOUR_OF_DAY, 8);
        compareTime.set(Calendar.MINUTE, 30);
        compareTime.set(Calendar.SECOND, 0);
        compareTime.set(Calendar.MILLISECOND,0);

        Calendar compareTime2 = Calendar.getInstance();
        compareTime2.set(Calendar.MONTH,curretTime.get(Calendar.MONTH)+1);
        compareTime2.set(Calendar.DAY_OF_MONTH, 1);
        compareTime2.set(Calendar.HOUR_OF_DAY, 8);
        compareTime2.set(Calendar.MINUTE, 30);
        compareTime2.set(Calendar.SECOND, 0);
        compareTime2.set(Calendar.MILLISECOND,0);

        shiftDate[0] = compareTime.getTime();
        shiftDate[1] = compareTime2.getTime();

        return  shiftDate;
    }

    public static Date[] getShiftDateLPA()
    {
        Date[] shiftDate = new Date[2];
        Calendar curretTime = Calendar.getInstance();

        Calendar compareTime = Calendar.getInstance();
        compareTime.add(Calendar.DAY_OF_MONTH,-30);
        compareTime.set(Calendar.HOUR_OF_DAY, 8);
        compareTime.set(Calendar.MINUTE, 30);
        compareTime.set(Calendar.SECOND, 0);
        compareTime.set(Calendar.MILLISECOND,0);

        Calendar compareTime2 = Calendar.getInstance();
        compareTime2.set(Calendar.HOUR_OF_DAY, 20);
        compareTime2.set(Calendar.MINUTE, 30);
        compareTime2.set(Calendar.SECOND, 0);
        compareTime2.set(Calendar.MILLISECOND,0);

        shiftDate[0] = compareTime.getTime();
        shiftDate[1] = compareTime2.getTime();

        return shiftDate;
    }
    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
    
    /**
           * 计算两个时间差多少分钟
     */
    public static int getDatePoorMinute(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少分钟
        Long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return min.intValue();
    }




    /**
     * @Description: 当月剩余天数(不包括当天,1月20日 剩余11天)
     * @param
     * @return: java.lang.Integer
     * @Author: 唐子越
     * @Date: 2023/7/14 9:18
     */
    public static Integer monthEndNum(){

        Date dateTime = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        int today = c.get(Calendar.DAY_OF_MONTH);
        int last = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        return last - today;
    }


    /*
     * @Description: 获得当前时本月中几号
     * @param
     * @return: java.lang.Integer
     * @Author: 唐子越
     * @Date: 2023/7/14 13:42
     */
    public static Integer dayOfMonth(){
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }
}
